package com.lys.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.blog.dao.mapper.TagMapper;
import com.lys.blog.dao.pojo.Tag;
import com.lys.blog.service.TagService;
import com.lys.blog.vo.Result;
import com.lys.blog.vo.TagVo;
import com.lys.blog.vo.params.PageParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    private TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        return tagVo;
    }

    public List<TagVo> copyList(List<Tag> tagList) {
        List<TagVo> tagVos = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVos.add(copy(tag));
        }
        return tagVos;
    }


    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        //mybatis-plus无法 进行多表查询
        List<Tag> tags=tagMapper.findTagsByArticleId(articleId);
        return copyList(tags);
    }

    @Override
    public Result hots(int limit) {
    //    标签拥有的文章是最多的
    //    查询根据tag_id分组，计数，倒序排列，从大到小，取前limit个
        List<Long> tagIds = tagMapper.findHotsTagIds(limit);
        if (CollectionUtils.isEmpty(tagIds)) {
            return Result.success(Collections.emptyList());
        }
    //    需求的是tagID和tagName的tag对象
    //    select * from tag where id in(1,2,3,4)
        List<Tag> tagList=tagMapper.findTagsByTagIds(tagIds);
        return Result.success(tagList);

    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId, Tag::getTagName);
        List<Tag> tags = tagMapper.selectList(queryWrapper);
        return Result.success(copyList(tags));
    }

    @Override
    public Result tagsDetails() {
        List<Tag> tags = tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tags));
    }

    @Override
    public Result tagsDetailsById(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = copy(tag);
        return Result.success(tagVo);
    }

    @Override
    public Result addTags(Tag tag) {
        System.out.println(tag);
        int insert = tagMapper.insert(tag);
        System.out.println(tag);
        return Result.success(tag.getId());
    }

    @Override
    public Result deleteTags(Long id) {
        int i = tagMapper.deleteById(id);
        if (i > 0) {
            return Result.success(null);
        } else {
            return Result.fail(198, "删除失败");
        }
    }

    @Override
    public Result modifyTags(Tag tag) {
        int i = tagMapper.updateById(tag);
        if (i > 0) {
            return Result.success(null);
        } else {
            return Result.fail(198, "删除失败");
        }
    }

    @Override
    public Result listByPage(PageParams page) {
        System.out.println(page.getPage());
        System.out.println(page.getPageSize());
        Page<Tag> tagPage = new Page<>(page.getPage(), page.getPageSize());
        return Result.success(tagMapper.selectPage(tagPage, null));
    }

}
